package com.lemon.smartsafetynew.ui.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.ApplicationInfo;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.net.services.AccountServices;
import com.lemon.smartsafetynew.ui.activity.MainNewActivity;
import com.lemon.smartsafetynew.ui.adapter.user.UserAccountChooseAdapter;
import com.lemon.smartsafetynew.util.common.AppSharePreferenceUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.constants.SafeCloudConstants;
import com.lemon.smartsafetynew.util.user.UserTypeUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import kale.adapter.LemonRcvAdapter;

/**
 * 用户选择登录企业
 * Created by Andrew on 2017/9/27.
 */

public class UserAccountChooseActivity extends SwipeBackActivity
        implements RecyclerTClickListener<User> {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    private LemonRcvAdapter<User> mAdapter;
    private List<User> mDataList = new ArrayList<>();

    private static String mPassword;
    private static String mMobile;

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {

            if (bundle.containsKey(KeyConstants.KEY_USER_LIST)) {
                String string = bundle.getString(KeyConstants.KEY_USER_LIST);
                mDataList = new Gson().fromJson(string, new TypeToken<List<User>>() {
                }.getType());
            }

            if (bundle.containsKey(KeyConstants.KEY_USER_PD)) {
                mPassword = bundle.getString(KeyConstants.KEY_USER_PD);
            }

            if (bundle.containsKey(KeyConstants.KEY_USER_ACCOUNT)) {
                mMobile = bundle.getString(KeyConstants.KEY_USER_ACCOUNT);
            }
        }
    }

    @Override
    protected void initTitle() {
        this.setBarTitle(getString(R.string.please_choose_login_organization));
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(UserAccountChooseActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_user_account_choose;
    }

    @Override
    protected void initViews() {
        super.initViews();

        initRecyclerView();

    }

    /**
     * 初始化页面组件
     */
    private void initRecyclerView() {
        mAdapter = new UserAccountChooseAdapter(mDataList, this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(UserAccountChooseActivity.this, 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onItemClick(User data, int position) {

        AccountServices
                .init()
                .doLogin(mMobile, mPassword, data.getUserId());

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThread(HttpCallbackEvent<List<User>> event) {
        if (KeyConstants.URL_ACCOUNT_LOGIN.equals(event.getTag())) {
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
                mHUDUtil.showHUD();
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                mHUDUtil.hidden();

                if (event.getEventResult() == Constants.Result.SUCCESS) {

                    List<User> users = event.getObj();

                    if(users != null && !users.isEmpty()) {

                        //  企事业人员/政府机关单位可以登陆条件：
                        //  条件一：企事业单位登陆人的companyId不为能为空
                        //  条件二：用户类型是产品支持
                        //  条件三：用户类型是政府人员
                        User user = users.get(0);
                        if(user.getCompanyId() != null
                                || (user.getCompanyId() == null && ApplicationInfo.PRODUCT_SUPPORT.equals(user.getUserType()))
                                || (UserTypeUtil.isGovType(user.getUserType()))) {
                            Toast.makeText(this, getString(R.string.login_success), Toast.LENGTH_SHORT).show();
                            AppSharePreferenceUtil.getInstance().putString(SafeCloudConstants.CURRENT_USER, mGson.toJson(user));
                            Intent mIntent = new Intent(UserAccountChooseActivity.this, MainNewActivity.class);
                            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(mIntent);
                        } else {
                            Toast.makeText(this, "公司为空，请联系管理员。", Toast.LENGTH_SHORT).show();
                        }

                    } else {
                        Toast.makeText(this, "登陆失败。", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    Toast.makeText(this, "登陆失败。", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

}
